﻿using project.B12.IRepository;
using project.B12.IRepository.Base;
using project.B12.Model.Models;
using project.B12.Repository.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project.B12.Repository
{
    public class ResultReportedRepository : SugarRepository<Table_ResultReported>, IResultReportedRepository
    {
        private IUnitOfWork UOW;

        public ResultReportedRepository(IUnitOfWork pUOW) : base(pUOW)
        {
            UOW = pUOW;
        }

        public void AddReport(Table_ResultReported report)
        {
            try
            {
                UOW.BeginTran();
                Add(report);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }

        /// <summary>
        /// 查找结果报告
        /// </summary>
        /// <param name="plateType">板类型</param>
        /// <param name="plateId">板ID</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Table_ResultReported> QueryReport(string plateType, string plateId)
        {
            List<Table_ResultReported> result;
            try
            {
                UOW.BeginTran();
                result = Query(o => o.PlateType==plateType&&o.PlateId==plateId).Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        /// <summary>
        /// 根据时间查询结果报告
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Table_ResultReported> QueryReportByTime(DateTime? startTime, DateTime? endTime)
        {
            List<Table_ResultReported> result=new List<Table_ResultReported>();
            try
            {
                UOW.BeginTran();
                if (startTime != null && endTime != null)
                {
                    if(startTime==endTime)
                        endTime=startTime?.AddDays(1);
                    result = Query(o => o.LoadTime >= startTime && o.LoadTime <= endTime).Result;
                }
                else if (startTime != null)
                    result = Query(o => o.LoadTime >= startTime).Result;
                else if (endTime != null)
                    result = Query(o => o.LoadTime <= endTime).Result;
                else
                    result = Query().Result;
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
            return result;
        }

        public void UpdateReport(Table_ResultReported report)
        {
            try
            {
                UOW.BeginTran();
                Update(report);
                UOW.CommitTran();
            }
            catch (Exception ex)
            {
                UOW.RollbackTran();
                throw ex;
            }
        }
    }
}
